<?php

require '../utils/conn.php';
include_once '../models/machine.php';

function getMachineByExamAndType($exam, $type) {

	$query = "SELECT * FROM t_machine WHERE type = ".$type." and exam_type=".$exam;
	$result = mysql_query($query)  or die("查询单词出现错误：". mysql_error());
	$machines = array();

	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$machine = new Machine();
		$machine->setMachineId($row['m_id']);
		$machine->setTitle($row['title']);
		$machine->setUpdate($row['update_info']);
		$machine->setMachineType($row['type']);
		$machine->setContent($row['content']);
		$content = str_replace("\"","'",$machine->content);
		$machine->setContent($content);
		$content = strip_tags($content);
		$contentLength = strlen($content);
		if ($contentLength > 100) {
			$machine->setDescription(substr($content, 0, 100));
		}
		else{
			$machine->setDescription($content);
		}
		$machine->setExamtype($row['exam_type']);
		$machine->setTimestamp($row['timestamp']);
		$machines[] = $machine;
	}
	return $machines;
}

function getMachineBaseInfoByExamAndType($exam, $type) {

	$query = "SELECT m_id,title,update_info,type,exam_type,timestamp FROM t_machine WHERE type = ".$type." and exam_type=".$exam;
	$result = mysql_query($query)  or die("查询单词出现错误：". mysql_error());
	$machines = array();

	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$machine = new Machine();
		$machine->setMachineId($row['m_id']);
		$machine->setTitle($row['title']);
		$machine->setUpdate($row['update_info']);
		$machine->setMachineType($row['type']);
		$machine->setExamtype($row['exam_type']);
		$machine->setTimestamp($row['timestamp']);
		$machines[] = $machine;
	}
	return $machines;
}

function getMachineById($machineId){
	$query = "SELECT * FROM t_machine WHERE m_id ='$machineId'";
	$result = mysql_query($query)  or die("查询单词出现错误：". mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$machine = new Machine();
	$machine->setMachineId($row['m_id']);
	$machine->setTitle($row['title']);
	$machine->setUpdate($row['update_info']);
	$machine->setMachineType($row['type']);
	$machine->setContent($row['content']);
	$content = str_replace("\"","'",$machine->content);
	$machine->setContent($content);
	$content = strip_tags($content);
	$contentLength = strlen($content);
	if ($contentLength > 100) {
		$machine->setDescription(substr($content, 0, 100));
	}
	else{
		$machine->setDescription($content);
	}
	$machine->setExamtype($row['exam_type']);
	$machine->setTimestamp($row['timestamp']);

	return $machine;
}

?>